<template>
	<view class="my-settle-container">
		<label class="radio" @click="changeAllStatus">
			<radio color="#c00000" :checked="isAllChecked" /><text>全选</text>
		</label>
		<view class="amount-box">
			合计：<text class="amount">￥{{totalPrice}}</text>
		</view>
		<view class="btn-settle" @click="settleMent">
			结算({{checkCount}})
		</view>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-settle",
		computed: {
			...mapGetters('cart', ['checkCount', 'totalPrice', 'isAllChecked']),
			...mapGetters('user', ['addressStr']),
			...mapState('user', ['token']),
			...mapState('cart', ['cart'])
		},
		data() {
			return {
				second: 3,
				timer: null
			};
		},
		methods: {
			...mapMutations('cart', ['updateAllGoodsStatus']),
			...mapMutations('user', ['upadateRedirectInfo']),
			changeAllStatus() {
				this.updateAllGoodsStatus(!this.isAllChecked)
			},
			settleMent() {
				if (!this.checkCount) return uni.$showMsg('请选择商品')
				if (!this.addressStr) return uni.$showMsg('请选择收货地址')
				if (!this.token) this.delayNavigate()
				this.payOrder()
			},
			showTips(n) {
				uni.showToast({
					title: '请先登录后在结算！' + n + '秒后自动跳装到登录页',
					icon: 'none',
					mask: true,
					duration: 1500
				})
			},
			delayNavigate() {
				this.second = 3
				this.timer = setInterval(() => {
					this.showTips(this.second)
					this.second--
					if (this.second < 0) {
						clearInterval(this.timer)
						uni.switchTab({
							url: '/pages/my/my',
							success: () => {
								this.upadateRedirectInfo({
									opentype: 'switchTab',
									from: '/pages/cart/cart'
								})
							}
						})
					}
				}, 1000)
			},
			async payOrder() {
				const orderInfo = {
					order_price: 0.01,
					consignee_addr: this.addressStr,
					goods: this.cart.filter(item => item.goods_status).map(item => ({
						goods_id: item.goods_id,
						goods_number: item.goods_count,
						goods_price: item.goods_price
					}))
				}
				const res = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
				if (res.data.meta.status !== 200) uni.$showMsg('订单创建失败')
				const orderNumber = res.data.message.order_number

				const res2 = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', {
					order_number: orderNumber
				})
				if (res2.data.meta.status !== 200) return uni.$showMsg('预付订单创建失败')
				const payInfo = res2.data.message.pay

				const [err, succ] = await uni.requestPayment(payInfo)
				if (err) return uni.$showMsg('订单未支付')
				const res3 = await uni.$http.post('/api/public/v1/my/orders/chkOrder', {
					order_number: orderNumber
				})
				if (res3.data.meta.status !== 200) return uni.$showMsg('订单未支付')
				uni.$showToast({
					icon: 'success',
					title: '订单支付成功'
				})
			}
		}
	}
</script>

<style lang="scss">
	.my-settle-container {
		position: fixed;
		left: 0;
		bottom: 0;
		height: 50px;
		width: 100%;
		z-index: 99;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding-left: 5px;
		font-size: 14px;
		background-color: #fff;

		.radio {
			display: flex;
			align-items: center;
		}

		.amount-box {
			.amount {
				color: #c00000;
				font-weight: bold;
			}

		}

		.btn-settle {
			background-color: #c00000;
			color: #fff;
			height: 100%;
			line-height: 50px;
			text-align: center;
			padding: 0 10px;
			min-width: 100px;
		}
	}
</style>